<?php defined("MAIN_DIR") or die("No direct access");

$bank_id = UV::readInt($route->getPath(4));

$edit_error = false;
if ($route->POST('action') == "edit")
{
	$link = filter_var($_POST['link'],FILTER_SANITIZE_MAGIC_QUOTES);
	$name = filter_var($_POST['name'],FILTER_SANITIZE_MAGIC_QUOTES);
	$full_name = filter_var($_POST['full_name'],FILTER_SANITIZE_MAGIC_QUOTES);
	$parent = $_POST['parent'] ? filter_var($_POST['parent_bank'],FILTER_SANITIZE_MAGIC_QUOTES) : 0;
	$address = filter_var($_POST['address'],FILTER_SANITIZE_MAGIC_QUOTES);
	$about = filter_var($_POST['about'],FILTER_SANITIZE_MAGIC_QUOTES);
	$info = trim(filter_var($_POST['info'],FILTER_SANITIZE_MAGIC_QUOTES));
	
	if ($db->getCell("select `bank_id` from `bank` where `bank_id` != '$bank_id' and `link` = '$link' and `city_id` = '$_POST[city]' limit 1")) {
		$link_error = true;
	} else {
		$edit_query = "update `bank` set `link`='$link',`name`='$name',`full_name`='$full_name',`city_id`='$_POST[city]',`parent`='$parent',`license`='$_POST[license]',`cbrf_link`='$_POST[cbrf_link]',`address`='$address',`phone`='$_POST[phone]',`site`='$_POST[site]',`about`='$about',`info`='$info' where `bank_id` = '$_POST[bank_id]' limit 1";
		if (!mysql_query($edit_query))
			$edit_error = true;
	}
}

if ($bank_id) {
	$bank_res = mysql_query("select `B`.*,`C`.`path` from `bank` as `B`,`city` as `C` where `B`.`bank_id` = '$bank_id' and `B`.`city_id` = `C`.`city_id` limit 1");
	if (mysql_num_rows($bank_res) == 0) {
		to("/admin/banks/");
	}
	$bank_row = mysql_fetch_array($bank_res);
}

require_once ADMIN.HEADFILE;
?>
<script type='text/javascript' src='/files/js/admin/banks.js'></script>
<div align="left">
<h1>Редактирование банка</h1>
<hr /><?php
if (isset($_POST['action']) && $_POST['action'] == "edit" && !$edit_error)
	echo "<p style='color:green'>Успешно отредактировано</p>";
elseif ($edit_error)
	echo "<p style='color:red'>Не удалось отредактировать</p>";
if (isset($link_error))
	echo "<p style='color:red'>Банк с такой ссылкой в данном городе уже присутствует</p>";
?><form method='POST'>
<input type='hidden' name='action' value='edit' />
<input type='hidden' name='bank_id' value='<?php echo $bank_row['bank_id']; ?>' />
<table cellpadding='5' cellspacing='0'>
	<tr>
		<td width="150px">Ссылка на сайте</td>
		<td width="500px"><a href='<?php echo "/".$bank_row['path']."/banks/".$bank_row['link'].".html"; ?>' target="_blank"><?php echo $bank_row['name'];?></a></td>
	</tr>
	<tr>
		<td>ID</td>
		<td><input style="width:200px" type='text' name='bank_id' value='<?php echo $bank_row['bank_id'];?>' disabled /></td>
	</tr>
	<tr>
		<td>Ссылка</td>
		<td><input style="width:200px" type='text' name='link' value='<?php echo $bank_row['link'];?>' /> .html</td>
	</tr>
	<tr>
		<td><input type='radio' name='parent' class='parent' value='0' <?php if ($bank_row['parent'] == '0') echo "checked='checked'"; ?> /> Головной офис</td>
		<td>
			<input type='radio' name='parent' class='parent' value='1' <?php if ($bank_row['parent'] != '0') echo "checked='checked'"; ?> /> Филиал банка
			<select id='parent_bank' name='parent_bank' <?php if ($bank_row['parent'] == '0') echo "disabled='disabled'"; ?>><?php
				$main_banks_res = mysql_query("select * from `bank` where `parent` = '0' order by `name`");
				while($main_banks_row = mysql_fetch_array($main_banks_res))
				{
					echo "<option value='$main_banks_row[bank_id]'";
					if ($bank_row['parent'] == $main_banks_row['bank_id'])
						echo " selected='selected'";
					echo ">".$main_banks_row['name']."</option>";
				}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Название</td>
		<td><input style='width:100%' type='text' name='name' id='name' value='<?php echo $bank_row['name']; ?>' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?> /></td>
	</tr>
	<tr>
		<td>Полное название</td>
		<td><input style='width:100%' type='text' name='full_name' id='fullname' value='<?php echo $bank_row['full_name'];?>' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?> /></td>
	</tr>
	<tr>
		<td>Город</td>
		<td>
			<select style="width:200px" name='city'><?php
			$city_res = mysql_query("select `city_id`,`city_name` from `city` order by `city_id`");
			while ($city_row = mysql_fetch_array($city_res))
			{
				echo "<option value='$city_row[city_id]'";
				if ($city_row['city_id'] == $bank_row['city_id'])
					echo " selected='selected'";
				echo ">$city_row[city_name]</option>";
			}
			?></select>
		</td>
	</tr>
	<tr>
		<td>Лицензия</td>
		<td><input style="width:200px" type='text' name='license' id='license' value='<?php echo $bank_row['license'];?>' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?> /></td>
	</tr>
	<tr>
		<td>Ссылка на ЦБ РФ</td>
		<td><input style="width:200px" type='text' name='cbrf_link' id='cbrf_link' value='<?php echo $bank_row['cbrf_link'];?>' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?> /></td>
	</tr>
	<tr>
		<td>Адрес</td>
		<td><input style='width:100%' type='text' name='address' value='<?php echo $bank_row['address'];?>' /></td>
	</tr>
	<tr>
		<td>Телефон</td>
		<td><input style='width:100%' type='text' name='phone' value='<?php echo $bank_row['phone'];?>' /></td>
	</tr>
	<tr>
		<td>Сайт</td>
		<td><input style='width:100%' type='text' name='site' id='site' value='<?php echo $bank_row['site'];?>' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?> /></td>
	</tr>
	<tr valign='top'>
		<td>О банке</td>
		<td><textarea style='width:100%' rows='5' name='about' id='about' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?>><?php echo $bank_row['about'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td>Инфо</td>
		<td><textarea style='width:100%' rows='5' name='info' id='info' <?php if ($bank_row['parent'] != '0') echo "readonly='readonly'"; ?>><?php echo $bank_row['info'];?></textarea></td>
	</tr>
	<tr valign='top'>
		<td colspan='2'>
			<p><input type='button' id="insert_city" value='Вставить' <?php if ($bank_row['parent'] != '0') echo "disabled='disabled'"; ?> /> - Вставить название города<br />
			<input type='button' id="insert_city_rod" value='Вставить' <?php if ($bank_row['parent'] != '0') echo "disabled='disabled'"; ?> /> - Вставить название города в родительном падеже<br />
			<input type='button' id="insert_city_pred" value='Вставить' <?php if ($bank_row['parent'] != '0') echo "disabled='disabled'"; ?> /> - Вставить название города в предложном падеже<br />
			</p>
		</td>
	</tr>
	<tr valign='top'>
		<td colspan='2' align="right"><button style="width:100px;height:30px" type="submit">Сохранить</button></td>
	</tr>
</table>
</form>
<br /><hr /><br />
</div>

<?php require_once ADMIN.FOOTERFILE; ?>